package com.ruoyi.system.controller;

import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.RecruitCv;
import com.ruoyi.system.service.IRecruitCvService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 简历筛选Controller
 * 
 * @author ruoyi
 * @date 2023-11-14
 */
@RestController
@RequestMapping("/system/cv")
public class RecruitCvController extends BaseController
{
    @Autowired
    private IRecruitCvService recruitCvService;


    @Anonymous
    @GetMapping("/cvProgress")
    public AjaxResult getRecruitCvProgress(){
        List<RecruitCv> recruitCvs = recruitCvService.selectRecruitCvList(null);
        List<Map<String, Object>> resultList = new ArrayList<>();

        Map<String, Integer> map = new HashMap<>();
        map.put("已投", 0);
        map.put("通过", 0);
        map.put("拒绝", 0);

        for (RecruitCv cv : recruitCvs) {
            if (cv.getProgress().equals("0")) {
                map.put("已投", map.get("已投") + 1);
            } else if (cv.getProgress().equals("2")) {
                map.put("通过", map.get("通过") + 1);
            } else if (cv.getProgress().equals("3")) {
                map.put("拒绝", map.get("拒绝") + 1);
            }
        }

        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            Map<String, Object> item = new HashMap<>();
            item.put("value", entry.getValue());
            item.put("name", entry.getKey());
            resultList.add(item);
        }
        // 反转resultList数组
        Collections.reverse(resultList);


        return AjaxResult.success(resultList);
    }

    /**
     * 查询简历筛选列表
     */
    @PreAuthorize("@ss.hasPermi('system:cv:list')")
    @GetMapping("/list")
    public TableDataInfo list(RecruitCv recruitCv)
    {
        startPage();
        List<RecruitCv> list = recruitCvService.selectRecruitCvList(recruitCv);
        return getDataTable(list);
    }

    /**
     * 导出简历筛选列表
     */
    @PreAuthorize("@ss.hasPermi('system:cv:export')")
    @Log(title = "简历筛选", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RecruitCv recruitCv)
    {
        List<RecruitCv> list = recruitCvService.selectRecruitCvList(recruitCv);
        ExcelUtil<RecruitCv> util = new ExcelUtil<RecruitCv>(RecruitCv.class);
        util.exportExcel(response, list, "简历筛选数据");
    }

    /**
     * 获取简历筛选详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:cv:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(recruitCvService.selectRecruitCvById(id));
    }

    /**
     * 新增简历筛选
     */
    @PreAuthorize("@ss.hasPermi('system:cv:add')")
    @Log(title = "简历筛选", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RecruitCv recruitCv)
    {
        return toAjax(recruitCvService.insertRecruitCv(recruitCv));
    }

    /**
     * 修改简历筛选
     */
    @PreAuthorize("@ss.hasPermi('system:cv:edit')")
    @Log(title = "简历筛选", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RecruitCv recruitCv)
    {
        return toAjax(recruitCvService.updateRecruitCv(recruitCv));
    }

    /**
     * 删除简历筛选
     */
    @PreAuthorize("@ss.hasPermi('system:cv:remove')")
    @Log(title = "简历筛选", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(recruitCvService.deleteRecruitCvByIds(ids));
    }
}
